package com.bhs.sansonglogistics.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.bean.TypeBean;
import com.bhs.sansonglogistics.ui.adapter.RefuseAdapter;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;

/**
 * 费用类型
 */
public class FeeTypeDialog extends Dialog implements View.OnClickListener {

    private RecyclerView mRvList;
    private EditText mEtReason;
    private RefuseAdapter mAdapter;

    public FeeTypeDialog(@NonNull Context context) {
        super(context, R.style.MyBottomDialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bottom_refuse);
        Window window = getWindow();
        //底部弹出的Dialog
        window.setGravity(Gravity.BOTTOM);
        //设置动画
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams attributes = window.getAttributes();

        //保证按钮水平满屏
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        //设置显示位置
        window.setAttributes(attributes); //将属性设置给窗体
        initView();
    }

    private void initView() {

        findViewById(R.id.iv_close).setOnClickListener(this);
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("费用类型");
        mRvList = findViewById(R.id.rv_list);
        mEtReason = findViewById(R.id.et_reason);
        mEtReason.setHint("请输入费用类型");
        findViewById(R.id.btn_confirm).setOnClickListener(this);
        ArrayList<TypeBean> list = new ArrayList<>();
        list.add(new TypeBean("搬运费", 0));
        list.add(new TypeBean("卸货费", 0));
        list.add(new TypeBean("过路费", 0));
        list.add(new TypeBean("等待费", 0));
        list.add(new TypeBean("超时费", 0));
        list.add(new TypeBean("其他", 0));
        mAdapter = new RefuseAdapter(R.layout.item_refuse, list);
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (TypeBean typeBean : list) {
                    typeBean.setChecked(false);
                }
                list.get(position).setChecked(true);
                if (list.get(position).getTypeName().equals("其他")) {
                    mEtReason.setVisibility(View.VISIBLE);
                } else {
                    mEtReason.setVisibility(View.GONE);
                }
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onClick(View view) {
        dismiss();
        if (view.getId() == R.id.btn_confirm) {
            if (mEtReason.getVisibility() == View.VISIBLE) {
                if (TextUtils.isEmpty(mEtReason.getText())) {
                    ToastUtil.show("请输入其他费用");
                    return;
                }
                myClickListener.onClick("", mEtReason.getText().toString());
            } else {
                for (TypeBean typeBean : mAdapter.getData()) {
                    if (typeBean.isChecked()) {
                        myClickListener.onClick("", typeBean.getTypeName());
                        break;
                    }
                }
            }
        }
    }

    private MyClickListener2 myClickListener;

    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.myClickListener = l;
    }

}
